<?php
namespace app\common\model;

use think\Model;

class SystemItem extends Model
{
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


	protected $type = [
        'setup'      =>  'json',
    ];


	//关联管理员组
	public function group()
    {
        return $this->belongsTo(SystemGroup::class);
    }

    public function getStatusTextAttr($value,$data)
    {
        $status = [0=>'禁用',1=>'正常'];
        return $status[$data['status']];
    }

	//获取管理员组
    public function getGroupNameAttr($value,$data)
    {
		$group = $this->group->getData('name');
        return $group;
    }

	//获取管理员组
    public function getTypeNameAttr($value,$data)
    {
		$type = self::getType($data['type']);
        return $type;
    }

	// 获取字段选项
    public function getSetupOptionsAttr($value,$data)
    {
		$setup = json_decode($data['setup']);
		$options = $setup->options;
		if(!empty($options)){
			$arr = explode("\r\n",$options);
			$options = [];
			foreach($arr as $key => $val){
				$op = explode("|",$val);
				$options[$key]['val'] = $op[1];
				$options[$key]['title'] = $op[0];
			}
		}

		return $options;
    }


    // 字段类型
    public static function getType($type=''){
        $arr=[
            'text'      => '单行文本',
            'textarea'  => '多行文本',
            'editor'    => '编辑器',
            'select'    => '下拉列表',
            'radio'     => '单选按钮',
            'checkbox'  => '复选框',
            'image'     => '单张图片',
            'file'      => '文件上传',
            'datetime'  => '日期和时间',
        ];
        if ($type) {
            return $arr[$type];
        } else {
            return $arr;
        }
    }

}